(function() {
    angular.module("webApp").controller("SubscribemessageCtrl", SubscribemessageCtrl);
    SubscribemessageCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location', '$rootScope', 'sessionService'];

    function SubscribemessageCtrl($scope, dataService, helper, $state, $location, $rootScope, sessionService) {
        //根据模板标题，拉取模板关键词
        $scope.titleCheck = function(title) {
            if (title) {
                title.store_id = $rootScope.store_id;
            }

            dataService.templateAddNew(title).then(function(result) {
                if (result.Msg == "ok") {
                    helper.message("模板创建成功，请添加至个人模板列表！");
                    $scope.getSubscribemessageList(1);
                } else {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                }
            });
        }

        //已经拉取的模板列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getSubscribemessageList(1);
        }
        $scope.getSubscribemessageList = function(pageNo) {
            var query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id
            };
            dataService.subscribemessageGet(query).then(function(result) {
                if (result.total_count) {
                    $scope.template_list = result.template_list;
                    $scope.total_count = result.total_count;
                }
            });
        }

        // 添加模板
        $scope.addTemplateToPersonal = function(id, category, type) {
            if (!id) {
                helper.myAlert("error：参数不全，不能添加模板消息");
                return false;
            }

            if (!category) {
                helper.myAlert("error：没有选择模板分类，不能添加模板消息");
                return false;
            }

            if (!type) {
                helper.myAlert("error：没有选择模板类型，不能添加模板消息");
                return false;
            }

            // 
            if (!$scope.wxappid) {
                helper.myAlert("error：没有选择小程序，无法添加模板");
                return false;
            }

            var data = {
                "id": id,
                "category": category,
                "type": type,
                "store_id": $rootScope.store_id,
                "app_id": $scope.wxappid
            };

            dataService.addTemplateToPersonal(data).then(function(result) {
                if (result.Msg == "ok") {
                    helper.myAlert("success", "#/subscribemessage/mylist?app_id=" + $scope.wxappid);
                } else {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                }
            });
        }

        //商家账户下已添加的模板
        $scope.mylistAction = function() {
            $scope.app_id = $location.search().app_id;
            $scope.mylist();
        }

        $scope.mylist = function() {
            var query = {
                app_id: $scope.app_id,
                store_id: $rootScope.store_id
            };
            dataService.mySubscribemessageGet(query).then(function(result) {
                if (result.Msg == "ok") {
                    $scope.my_list = result.my_list;
                } else {
                    helper.message(result.Msg, "error");
                    return false;
                }
            });
        }

        //删除
        $scope.delTemplate = function(template_id) {
            helper.myConfirm("模板删除后，此前已订阅的将无法送消息，确定删除么？", function() {
                if (!$scope.app_id) {
                    helper.message("没有对应的小程序，无法删除！", "error");
                    return false;
                }

                if (!template_id) {
                    helper.message("没有模板编号，无法删除！", "error");
                    return false;
                }
                var data = {
                    app_id: $scope.app_id,
                    template_id: template_id,
                    store_id: $rootScope.store_id
                };
                dataService.delTemplate(data).then(function(result) {
                    if (result.Msg == "ok") {
                        helper.message("删除成功！");
                        $scope.mylist();
                    } else {
                        helper.message(result.Msg, "error");
                        return false;
                    }
                });
            });
        }
    }
})();